// pages/movie/movie.js

let app = getApp();
let utils = require("../util/utils.js");


Page({
    data: {
        comingSoom: [],
        inTheaters: [],
        Top250: []
    },
    onLoad: function (options) {
        const url = app.globalUrl.doubanUrl + '/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=3&page_start=0'
        this.http(url, this.callback, 'comingSoom', "正在热映")
        this.http(url, this.callback, 'inTheaters', "即将上映")
        this.http(url, this.callback, 'Top250', "排行榜")
    },
    // 封装请求函数
    http: function (url, callback, category, categoryName) {
        wx.request({
            url: url,
            header: {
                'content-type': 'application/xml' // 默认值
            },
            success(res) {
                callback(res.data, category, categoryName)
            }
        })
    },
    // 回调函数，处理数据
    callback: function (res, category, categoryName) {
        // console.log(res)
        // 处理数据 数据的过滤存储
        /**
         * 1.大图
         * 2.标题
         * 3.星星
         * 4.评分
         * 5.id
         */
        let movies = []
        for (let idx in res.subjects) {
            let subject = res.subjects[idx];

            let title = subject.title;
            if (title.length >= 6) {
                //  名字过长处理
                title = title.substring(0, 6) + "...";
            }

            let temp = {
                title: title,
                coverImgUrl: subject.cover,
                star: utils.convertToStarsArray(subject.rate * 10 / 2),
                average: subject.rate,
                movieid: subject.id
            }
            movies.push(temp)
        }

        // console.log(movies)
        var readyData = {}
        readyData[category] = {
            categoryName: categoryName,
            movies: movies
        }
        console.log(readyData)
        this.setData(readyData)
    },
    // 跳转到更多页面
    movieMoreTap: function (event) {
        const categoryName = event.currentTarget.dataset.categoryname;
        wx.navigateTo({
            url: './movie-more/movie-more?categoryname=' + categoryName
        })
    },

    goMovieDetail: function (event) {
        let movieId = event.currentTarget.dataset.movieid
        wx.navigateTo({
            url: './movie-detail/movie-detail?movieid=' + movieId,
        })
    },
})